<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <script>

        // 1 + 2 + 3 + 4 + 5 + 6

        // sum(5)+6    A同学  sum(5)
        // sum(4)+5      B同学  sum(4)
        // sum(3)+4
        // sum(2)+3
        // sum(1)+2     结束
        // sum(0)+1
        // sum(-1)+0  
        // ....

        function sum(n) {
            if (n === 1) return 1;
            return sum(n - 1) + n;
        }

        document.write(sum(5));



        // 阶乘
        // jc(10) = jc(9) * 10 ;
        // jc(9) = jc(8) * 9 ;
        // ..
        // jc(1) = 1   结束的条件

        function jc(n) {
            if (n === 1) return 1;
            return jc(n - 1) * n;
        }

        // document.write(jc());



    </script>

</body>

</html>